#ifndef NIXONBOT_CORE_BNCS_PROTOCOL_AUTHINFOSERVER_H_
#define NIXONBOT_CORE_BNCS_PROTOCOL_AUTHINFOSERVER_H_

#include <nixonbot/core/bncs/protocol/Message.h>
#include <nixonbot/core/bncs/protocol/MessageFactory.h>

namespace nixonbot {
namespace core {
namespace bncs {
namespace protocol {

struct AuthInfoServerData : public QSharedData
{
	quint32 logonType;
	quint32 serverToken;
	quint32 udpValue;
	quint32 mpqFileTime[2];
	QByteArray IX86FileName;
	QByteArray valueString;
	quint8 serverSignature[128];
};

class AuthInfoServer : public Message
{
public:
	static const int id = 0x50;
	BNCS_MESSAGE_SERVER_REGISTER(AuthInfoServer, id);

public:
	QSharedDataPointer<AuthInfoServerData> d;

public:
	AuthInfoServer()
	{
		d = new AuthInfoServerData();
	}
	
	virtual AuthInfoServer* create()
	{
		return new AuthInfoServer();
	}
	
	virtual quint32 id()
	{
		return id;
	}

	virtual int size()
	{
		return sizeof(d->logonType) + sizeof(d->serverToken) + sizeof(d->udpValue) +
		       sizeof(d->mpqFileTime) + (d->IX86FileName.size() + 1) + (d->valueString.size() + 1) +
		       sizeof(d->serverSignature);
	}

protected:
	virtual void readFrom(QDataStream& ds)
	{
		int bo = ds.byteOrder();
		ds.setByteOrder(QDataStream::LittleEndian);

		ds >> d->logonType;
		ds >> d->serverToken;
		ds >> d->udpValue;
		ds >> d->mpqFileTime[0];
		ds >> d->mpqFileTime[1];
		d->IX86FileName = readStringFrom(ds);
		d->valueString = readStringFrom(ds);
		ds.readRawData((char*)d->serverSignature, sizeof(d->serverSignature));

		ds.setByteOrder((QDataStream::ByteOrder)bo);
	}

	virtual void writeTo(QDataStream& ds)
	{
		int bo = ds.byteOrder();
		ds.setByteOrder(QDataStream::LittleEndian);

		ds << d->logonType;
		ds << d->serverToken;
		ds << d->udpValue;
		ds << d->mpqFileTime[0];
		ds << d->mpqFileTime[1];
		writeStringTo(ds, d->IX86FileName);
		writeStringTo(ds, d->valueString);
		ds.writeRawData((char*)d->serverSignature, sizeof(d->serverSignature));

		ds.setByteOrder((QDataStream::ByteOrder)bo);
	}
};

}
}
}
}

#endif /* NIXONBOT_CORE_BNCS_PROTOCOL_AUTHINFOSERVER_H_ */
